/**
 * 
 */
package vue;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tool.Language;
import controleur.SQCalculator;

/**
 * @author Patrick-Edouard
 *
 */
public class MotherFrame extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private double actualSQ;
	private Language la;
	private SQCalculator sq;
	private JPanel panel;
	private JLabel labelUnspend;
	private JLabel labelIncome;
	private JLabel labelSQ;
	private JTextField textUnspend;
	private JTextField textIncome;
	private JButton boutonCalcul;
	private JButton boutonSave;
	private JMenuBar menuBar;
	private JMenu menuSQ;
	private JMenu menuAbout;
	private JMenuItem menuItemAbout;
	private JMenuItem menuLang;
	private JMenuItem menuGraph;
	private JMenuItem menuSQList;
	
	public MotherFrame()
	{
		super();
		
		this.createWindow();
		
		this.setSq(new SQCalculator());
	}
	
	private void createWindow()
	{
		//window
		
		this.la = new Language();
		this.setTitle(this.la.getText("title"));
		this.setBounds(0, 0, 500, 200);
		
		
		//menus
		
		this.menuBar = new JMenuBar();
		this.menuSQ = new JMenu(this.la.getText("menu_sq"));
		this.menuAbout = new JMenu(this.la.getText("menu_about"));
		
		this.menuItemAbout = new JMenuItem(this.la.getText("menu_item_about"));
		this.menuItemAbout.addActionListener(this);
		this.menuLang = new JMenuItem(this.la.getText("menu_item_lang"));
		this.menuLang.addActionListener(this);
		this.menuGraph = new JMenuItem(this.la.getText("menu_item_graph"));
		this.menuGraph.addActionListener(this);
		this.menuSQList = new JMenuItem(this.la.getText("menu_item_list"));
		this.menuSQList.addActionListener(this);
		
		this.menuAbout.add(this.menuItemAbout);
		
		this.menuSQ.add(this.menuLang);
		this.menuSQ.add(this.menuGraph);
		this.menuSQ.add(this.menuSQList);
		
		this.menuBar.add(this.menuSQ);
		this.menuBar.add(this.menuAbout);
		
		this.setJMenuBar(this.menuBar);
		
		//things
		
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new GridLayout());
		
		//panels & buttons
		
		this.panel = new JPanel();
		
		this.labelIncome = new JLabel(this.la.getText("income"));
		this.labelUnspend = new JLabel(this.la.getText("unspend"));
		this.labelSQ = new JLabel();
		
		this.boutonCalcul = new JButton(this.la.getText("calcul"));
		this.boutonCalcul.addActionListener(this);
		this.boutonSave = new JButton(this.la.getText("main_save"));
		this.boutonSave.addActionListener(this);
		
		this.textIncome = new JTextField(10);
		this.textIncome.setBounds(50, 50, 50, 100);
		this.textUnspend = new JTextField(10);
		this.textUnspend.setBounds(50, 100, 50, 100);
		
		this.panel.add(this.labelIncome);
		this.panel.add(this.textIncome);
		this.panel.add(this.labelUnspend);
		this.panel.add(this.textUnspend);
		this.panel.add(this.boutonCalcul);
		this.panel.add(labelSQ);
		
		this.setContentPane(panel);
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.boutonCalcul)
		{
			this.boutonCalculClicked();
		}
		if(e.getSource()==this.boutonSave)
		{
			this.boutonSaveClicked();
		}
		if(e.getSource()==this.menuItemAbout)
		{
			this.menuAboutClicked();
		}
		if(e.getSource()==this.menuLang)
		{
			this.menuLangClicked();
		}
		if(e.getSource()==this.menuGraph)
		{
			this.menuGraphClicked();
		}
		if(e.getSource()==this.menuSQList)
		{
			this.menuSQListClicked();
		}
	}
	
	
	private void menuAboutClicked()
	{
		JDialog dialogAbout = new JDialog();
		dialogAbout.setTitle(this.la.getText("menu_item_about"));
		JLabel text = new JLabel();
		text.setText(this.la.getText("text_about"));
		dialogAbout.getContentPane().add(text);
		dialogAbout.pack();
		dialogAbout.setVisible(true);
	}
	
	private void menuLangClicked()
	{
		this.la.editLang();
	}
	
	private void menuGraphClicked()
	{
		double[] listY = getSq().getSQ();
		double[] listX = new double[listY.length];
		for(int i=0;i<listY.length;i++)
		{
			listX[i]=i+1;
		}
		new Graph(listX,listY);
	}
	
	private void menuSQListClicked()
	{
		new list(this);
	}
	
	private void boutonCalculClicked()
	{
		//if(!(this.textUnspend.getText()!="")&&(this.textIncome.getText()!=""))
		{
			this.actualSQ = this.getSq().calculSQ(Double.parseDouble(this.textUnspend.getText()), Double.parseDouble(this.textIncome.getText()));
			this.labelSQ.setText(this.la.getText("yoursq")+" "+this.actualSQ+"");
			this.panel.add(this.boutonSave);
		}
	}
	private void boutonSaveClicked()
	{
		this.getSq().saveSQ(this.actualSQ);
	}
	
	
	
	public static void main(String[] args) 
	{	
		if(args.length == 1)
		{			
			if(args[0]=="lang")
			{
				Language la = new Language();
				la.editLang();
			}
		}
		new MotherFrame();
	}

	/**
	 * @return the sq
	 */
	public SQCalculator getSq() {
		return sq;
	}

	/**
	 * @param sq the sq to set
	 */
	public void setSq(SQCalculator sq) {
		this.sq = sq;
	}
}
